<?PHP
session_start();
date_default_timezone_set("Asia/Chongqing");
require("../config/connect.php");
require("../public/common.php");
require("../public/biaoqing.php");

if( !HasLogin() ) 
{
	$url = "Location:login.php?ref=view_items.php";
	header($url);
	exit;
}

$email = $_SESSION["email"];
$userid = $_SESSION["userid"];

$input_tag = CheckUrlParamNoDie("input_tag");
$showtype = CheckUrlParamNoDie("showtype");
$opt = CheckUrlParamNoDie("opt");
$id = CheckUrlParamNoDie("id");
$nPage = SetUrlParamInt("page");
if( $nPage < 0 )
	$nPage = 0;
	
if ($opt == "del" && $id != "")
{
	mysql_query("delete from goods_items where id=$id");
	if (mysql_affected_rows() == 1)
		echo "<script>window.history.go(-1);</script>";
}
else if ($opt == "setsystag" && $id != "")
{
	$systag = urldecode(CheckUrlParamNoDie("systag"));
//	echo "update goods_items set sys_tag='$systag' where id=$id";
//	exit;
	mysql_query("update goods_items set sys_tag='$systag' where id=$id");
//	if (mysql_affected_rows() == 1)
//		echo "<script>window.history.go(-1);/script>";
}
else if ($opt == "setcommend" && $id != "")
{
	mysql_query("update goods_items set state=-1 where id=$id");
	$commend = urldecode(CheckUrlParamNoDie("commend"));
	$sql = "INSERT INTO tsj_admin_commend (item_id, r_desc,time) values($id, '$commend', now()) ON DUPLICATE KEY UPDATE r_desc='$commend',time=now()";
//	echo $sql;
//	exit;
	mysql_query($sql);
}
else if ($opt == "checkok" && $id != "")
{
	mysql_query("update goods_items set state=2,time=now() where id=$id");
	if (mysql_affected_rows() == 1)
		echo "<script>window.history.go(-1);</script>";
}
else if ($opt == "checkno" && $id != "")
{
	mysql_query("update goods_items set state=0 where id=$id");
	if (mysql_affected_rows() == 1)
		echo "<script>alert('更新成功!');window.history.go(-1);</script>";
}
else if ($opt == "checknopass" && $id != "")
{
	mysql_query("update goods_items set state=-1 where id=$id");
	if (mysql_affected_rows() == 1)
		echo "<script>alert('更新成功!');window.history.go(-1);</script>";
}
else if ($opt == "shownow" && $id != "")
{
	mysql_query("update goods_items set state=2,time=now() where id=$id");
	if (mysql_affected_rows() == 1)
		echo "<script>window.history.go(-1);</script>";
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
body,td,th {
	font-family: 宋体;
	font-size: 13px;
	color: #000;
}
body {
	background-color: #FFF;
}
.STYLE1 {
	font-size: x-large;
	font-weight: bold;
}
.STYLE2 {color: #FF0000}
-->
</style>
<script language="javascript">
function OnFind()
{
	var input_tag = document.getElementById("input_tag").value;
	var search_type = 0;
	if (document.getElementsByName("search_type")[1].checked)
		search_type = 1;
	else
		search_type = 0;

	window.location.href= "?input_tag=" + input_tag + "&st=" + search_type;
}

function OnSysTag(obj,id)
{
	window.location.href= "?input_tag=<?PHP echo $input_tag; ?>&page=<?PHP echo $nPage; ?>&showtype=<?PHP echo $showtype; ?>&opt=setsystag&id="+id+"&systag=" + encodeURIComponent(obj.value);
}
function OnSelectCommend(obj,id)
{
	window.location.href= "?input_tag=<?PHP echo $input_tag; ?>&page=<?PHP echo $nPage; ?>&showtype=<?PHP echo $showtype; ?>&opt=setcommend&id="+id+"&commend=" + encodeURIComponent(obj.value);
}
</script>
</head>

<body>
<!--<a href="add_trade.php?opt=add">添加记录</a>-->
<div align="center" class="STYLE1">推荐商品处理后台 <span class="STYLE2">当前用户：</span><?PHP echo $_SESSION["email"]; ?><span style="padding-left:10px;"><a href="logout.php">退出</a></span></div>
<br>
<table align="center" width="100%">
	<tr>
    	<td align="center">标签:<input type="radio" name="search_type" value="0" checked="checked" />&nbsp;商品ID:<input type="radio" name="search_type" value="1" />
    	  <input type="text" onclick="javascript:select();" value="<?PHP echo $input_tag; ?>" id="input_tag" size="20"/>
    <input type="button" onclick="javascript:OnFind();" value="查找" />
    &nbsp;&nbsp;
	<a href="?page=0&showtype=<?PHP echo $showtype; ?>">首页</a>&nbsp;&nbsp;
		<?PHP if ($nPage > 0) echo "<a href='?input_tag=".$input_tag."&page=".($nPage - 1)."&showtype=".$showtype."'>上一页</a>"; ?>&nbsp;&nbsp;    
	<a href="?input_tag=<?PHP echo $input_tag; ?>&page=<?PHP echo $nPage + 1; ?>&showtype=<?PHP echo $showtype; ?>">下一页</a>		&nbsp;&nbsp;&nbsp;&nbsp;
    <a href="?">默认显示</a> &nbsp;
    <?PHP if($_SESSION["right"] > 1000) { ?>
    <a href="?showtype=my">我的分享</a>    <a href="?showtype=needdeal"> 待审核</a>&nbsp;  <a href="?showtype=huodong">活动商品</a>
    <?PHP } ?>
    <a href="?showtype=nopass">未通过</a> &nbsp;
    </td>
   </tr>
</table>
<p align="center"><a href="edit_items.php">添加</a>&nbsp;&nbsp;
<?PHP if($_SESSION["right"] > 1000) echo "<a href='edit_items_s.php'>添加活动商品</a>"; ?>
</p>
<div><span style="color:#00CC66">绿色：</span>审核通过显示；<span style="color:#666;">无色：</span>审核，但不显示；<span style="color:#FF00FF">紫红色：</span>待审核;<span style="color:#FFCC66">黄色：</span>隐藏商品
<?PHP
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;商品概况：";
if($_SESSION["right"] > 1000)
{
	$tmp = mysql_fetch_assoc( mysql_query("select count(*) as tmp from goods_items where state=0") );	$tmp = $tmp["tmp"];
	echo "待审核商品数：$tmp"."；&nbsp;&nbsp;";
	$tmp = mysql_fetch_assoc( mysql_query("select count(*) as tmp from goods_items where state=2 and datediff(now(), time)<1") );	$tmp = $tmp["tmp"];
	echo "24小时显示商品数：$tmp"."；&nbsp;&nbsp;";
	$tmp = mysql_fetch_assoc( mysql_query("select count(*) as tmp from goods_items where state=2 and datediff(now(), time)<7") );	$tmp = $tmp["tmp"];
	echo "7天显示商品数：$tmp"."；&nbsp;&nbsp;";
}

//
$limit = " limit ". $nPage*20 .",20";
//$sql = " from tsj_user,tsj_admin_commend,goods_items where goods_items.user_id=tsj_user.id and goods_items.id=tsj_admin_commend.item_id";
$sql = " from tsj_user,goods_items where goods_items.user_id=tsj_user.id";

if ($input_tag)
{
	$searchtype = CheckUrlParamNoDie("st");
	if ($searchtype == 1)
		$sql .= " and goods_items.id=$input_tag";	
	else
		$sql .= " and tag like '%$input_tag%'";	
}

if ($showtype=="needdeal")
	$sql .= " and state=0";
else if ($showtype=="huodong")
	$sql .= " and state=3";
else if ($showtype=="nopass")
	$sql .= " and state=-1";
	
if ($_SESSION["right"] < 1000 || $showtype=="my")
	$sql .= " and user_id=".$userid;

$tmp = mysql_fetch_assoc( mysql_query("select count(*) as tmp ".$sql) );	$tmp = $tmp["tmp"];
echo "当前视图下商品数：$tmp"."；&nbsp;&nbsp;";
?>
</div>
<table width="100%" cellpadding="0" cellspacing="0" border="1" align="center">
	<tr>
    	<td height="25" width="50" align="center">ID</td>
        <td width="160" align="center">时间</td>
        <td width="80" align="center">商品名称</td>
   	  	<td width="60" align="center">系统标签</td>
      	<td width="100" 	align="center">用户标签</td>
      	<td width="200" 	align="center">预览</td>
      	<td width="50" align="center">销售奖励</td>
      	<td width="236" align="center">推荐说</td>
      	<td width="50" align="center">用户ID</td>
      	<td width="200" align="center">操作</td>
        <td width="100" align="center">审核不通过理由</td>
    </tr>
<?php
$sql = "select * ".$sql." order by goods_items.id desc".$limit;
//echo $sql;
$result = mysql_query( $sql );
$i = 0;
while( $result && $row = mysql_fetch_assoc( $result ))
{
?>
<tr <?PHP if ($i++ % 2 == 0) echo "bgcolor='#CCCCCC'";?> >
    <td height="25" width="50" align="center"><a href="../item/?id=<?PHP echo $row["id"]; ?>" target="_blank"><?PHP echo $row["id"]; ?></a></td>
    <td align="center" ><?PHP echo $row["time"]; ?></td>
    <td align="center" ><?PHP echo $row["item_title"]; ?></td>
    <td align="center" ><?PHP if ($row["sys_tag"] == "") { ?>
		<select name="systag" id="systag" size="1" onchange="OnSysTag(this,<?PHP echo $row["id"] ?>)">
	      <option value="" <?PHP if ($row["sys_tag"] == "") echo "selected='selected'"; ?>></option>
          <option value="衣服" <?PHP if ($row["sys_tag"] == "衣服") echo "selected='selected'"; ?> >衣服</option>
          <option value="鞋子" <?PHP if ($row["sys_tag"] == "鞋子") echo "selected='selected'"; ?> >鞋子</option>
          <option value="包包" <?PHP if ($row["sys_tag"] == "包包") echo "selected='selected'"; ?> >包包</option>
          <option value="美妆" <?PHP if ($row["sys_tag"] == "美妆") echo "selected='selected'"; ?> >美妆</option>
          <option value="配饰" <?PHP if ($row["sys_tag"] == "配饰") echo "selected='selected'"; ?> >配饰</option>
          <option value="家居" <?PHP if ($row["sys_tag"] == "家居") echo "selected='selected'"; ?> >家居</option>
        </select>
       <?PHP } else { echo $row["sys_tag"]; }?>
       </td>
    <td align="center" ><?PHP echo $row["tag"]; ?></td>
    <td align="center" ><img src="<?PHP echo $row["pic_url"]; ?>" width="200" /></td>
    <td align="center" title="商品价格：<?PHP echo $row["fee"]; ?>" ><?PHP echo round(0.1*$row["commission"],2); ?></td>
    <td align="center" ><?PHP echo ReplaceBiaoQing($row["r_desc"]); ?></td>
    <td align="center"><?PHP echo $row["nick_name"]; ?></td>
	<td align="center" style="line-height:30px;" bgcolor="<?PHP if ($row["state"]==0) echo "#FF00FF"; else if($row["state"]==2) echo "#00CC66"; else if($row["state"]==3) echo "#FFCC66"; else if($row["state"]==-1) echo "#FF0000"; ?>" >
    <?PHP 
		if ($row["state"]==3) 
			echo "<a href='edit_items_s.php?id=".$row["id"]."'>编辑</a></br>&nbsp;&nbsp";
		else 
		    echo "<a href='edit_items.php?id=".$row["id"]."'>编辑</a></br>&nbsp;&nbsp";
	?>
    <?PHP 
	if ($_SESSION["right"] > 1000)
	{
		if ($row["state"] == 0)
		{
			echo "<a href='?opt=checkok&id=".$row["id"]."' style='font-size:16px'>审核通过</a></br>&nbsp;&nbsp;";
		}
		else if ($row["state"] == -1)
		{
			echo "<a href='?opt=checkok&id=".$row["id"]."'>审核通过</a></br>&nbsp;&nbsp;";
		}
		else if ($row["state"] != 3)
		{
			echo "<a href='?opt=checkno&id=".$row["id"]."'>打回未审核</a></br>&nbsp;&nbsp;";
			echo "<a href='?opt=shownow&id=".$row["id"]."'>立即显示</a></br>&nbsp;&nbsp;";
		}
	}
	if ($_SESSION["right"] > 1000 || $userid == $row["user_id"])
		//echo "<a href='?opt=del&id=".$row["id"]."'>删除</a></br>&nbsp;&nbsp;";
		echo "<a href=\"javascript:if(confirm('确实要删除吗?'))location='?opt=del&id=".$row["id"]."'\">删除</a></br>&nbsp;&nbsp;";
	?>
    </td>
    <td align="center">
    <?PHP
    if ($_SESSION["right"] > 1000){ ?>
   	<select name="commend" id="commend" size="1" onchange="OnSelectCommend(this,<?PHP echo $row["id"] ?>)">
          <option value="">清空</option>
          <option value="种类重复">种类重复</option>
          <option value="图片涉及广告过多">图片涉及广告过多</option>
          <option value="风格不符">风格不符</option>
        </select>
    <?PHP } ?>
    <br />
	<?PHP
    if ($row["state"] == -1)
	{
		$res = mysql_query("select * from tsj_admin_commend where item_id=".$row["id"]." limit 1");
		if ($result && $row2 = mysql_fetch_assoc( $res ))
		{
			echo $row2["r_desc"]; 
		}
	}
	?>
    </td>
</tr>
<?PHP
}
?>
</table>

<table align="center" width="100%">
	<tr>
    	<td align="center">标签:
    	  <input type="text" onclick="javascript:select();" value="<?PHP echo $input_tag; ?>" id="input_tag" size="20"/>
    <input type="button" onclick="javascript:OnFind();" value="查找" />
    &nbsp;&nbsp;
	<a href="?page=0&showtype=<?PHP echo $showtype; ?>">首页</a>&nbsp;&nbsp;
		<?PHP if ($nPage > 0) echo "<a href='?input_tag=".$input_tag."&page=".($nPage - 1)."&showtype=".$showtype."'>上一页</a>"; ?>&nbsp;&nbsp;    
	<a href="?input_tag=<?PHP echo $input_tag; ?>&page=<?PHP echo $nPage + 1; ?>&showtype=<?PHP echo $showtype; ?>">下一页</a>		&nbsp;&nbsp;&nbsp;&nbsp;
    <?PHP if($_SESSION["right"] > 1000) { ?>
    <a href="?">默认显示</a> &nbsp;<a href="?showtype=my">我的分享</a>    <a href="?showtype=needdeal"> 待审核</a>&nbsp; <a href="?showtype=huodong">活动商品</a>
    <?PHP } ?>	
    <a href="?showtype=nopass">未通过</a> &nbsp;
    </td>
   </tr>
</table>

</body>
</html>
